﻿package com.test;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.export.JRXmlExporter;

/**
 * @author lzwei E-mail:lzwei@atsig.com
 * 
 * @version 创建时间：Dec 17, 2008 9:29:57 AM 类说明
 * 
 * @Description 导出报表类
 */
public class JasperPrintControl {

	public JasperPrintControl() {

	}

	/*
	 * 
	 * 导出单个报表为PDF
	 * 
	 * @param JasperPrint,OutputStream,reportDataList
	 *  @ 页面设置response.setContentType("application/pdf");
	 *  @ 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.pdf");
	 *  @ 页面设置response.setContentLength(bytes.length);
	 * 
	 */

	public byte[] generatePDF(String reportTitle, String reportFilePath,
			List reportDataList) throws Exception {
		Map parameters = new HashMap();
		parameters.put("ReportTitle", reportTitle);// 报表标题
		JRPdfExporter exporter = new JRPdfExporter();
		ByteArrayOutputStream oStream = new ByteArrayOutputStream();
		try {
			JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(
					reportDataList);

			JasperPrint jasperPrint = JasperFillManager.fillReport(
					reportFilePath, parameters, ds);
			exporter.setParameter(
					JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
					Boolean.FALSE);
			exporter
					.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter
					.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
			exporter.exportReport();
			byte[] bytes = oStream.toByteArray();
			return bytes;
		} catch (JRException e) {
			throw new Exception("Report Export Failed.");
		}
	}

	/*
	 * 
	 * 导出单个报表为HTML
	 * 
	 * @param JasperPrint,OutputStream,reportDataList
	 *  @ 页面设置response.setContentType("text/html");
	 *  @ 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.html");
	 *  @ 页面设置response.setContentLength(bytes.length);
	 * 
	 */

	public byte[] generateHtml(String reportTitle, String reportFilePath,
			List reportDataList) throws Exception {
		Map parameters = new HashMap();
		parameters.put("ReportTitle", reportTitle);
		JRHtmlExporter exporter = new JRHtmlExporter();
		ByteArrayOutputStream oStream = new ByteArrayOutputStream();
		try {
			JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(
					reportDataList);
			JasperPrint jasperPrint = JasperFillManager.fillReport(
					reportFilePath, parameters, ds);
			exporter.setParameter(
					JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
					Boolean.FALSE);
			exporter
					.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter
					.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
			exporter.exportReport();
			byte[] bytes = oStream.toByteArray();
			return bytes;
		} catch (JRException e) {
			e.printStackTrace();
			throw new Exception("Report Export Failed.");
		}
	}

	/*
	 * 
	 * 导出单个报表为XLS
	 * 
	 * @param JasperPrint,OutputStream,reportDataList
	 *  @ 页面设置response.setContentType("application/vnd.ms-excel");
	 *  @
	 * 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.xls");
	 *  @ 页面设置response.setContentLength(bytes.length);
	 * 
	 */

	public byte[] generateExcel(String reportTitle, String reportFilePath,
			List reportDataList) throws Exception {
		Map parameters = new HashMap();
		parameters.put("ReportTitle", reportTitle);
		JRXlsExporter exporter = new JRXlsExporter(); // Excel
		ByteArrayOutputStream oStream = new ByteArrayOutputStream();
		try {
			JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(
					reportDataList);
			JasperPrint jasperPrint = JasperFillManager.fillReport(
					reportFilePath, parameters, ds);
			exporter
					.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
			exporter.setParameter(
					JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
					Boolean.TRUE);
			exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
					Boolean.FALSE);
			exporter.setParameter(
					JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,
					Boolean.FALSE);
			exporter.setParameter(JRXlsExporterParameter.OUTPUT_FILE_NAME, "a.xls");
			exporter.exportReport();
			byte[] bytes = oStream.toByteArray();
			return bytes;

		} catch (Exception e) {
			throw new Exception("Report Export Failed.");
		}
	}

	/*
	 * 
	 * 导出单个报表为rtf或word
	 * 
	 * @param JasperPrint,OutputStream,reportDataList
	 *  @ 页面设置response.setContentType("application/msword");
	 *  @
	 * 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.doc");
	 *  @ 页面设置response.setContentLength(bytes.length);
	 * 
	 */

	public byte[] generateWordOrRtf(String reportTitle, String reportFilePath,
			List reportDataList) throws Exception {
		Map parameters = new HashMap();
		parameters.put("ReportTitle", reportTitle);// 报表标题
		JRRtfExporter rtfExporter = new JRRtfExporter();// word Rtf
		ByteArrayOutputStream oStream = new ByteArrayOutputStream();
		try {
			JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(
					reportDataList);

			JasperPrint jasperPrint = JasperFillManager.fillReport(
					reportFilePath, parameters, ds);

			rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT,
					jasperPrint);
			rtfExporter
			.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
			//rtfExporter.setParameter(JRRtfExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);// 显示边框
			rtfExporter
					.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
			
			rtfExporter.exportReport();
			byte[] bytes = oStream.toByteArray();
			return bytes;
		} catch (JRException e) {
			e.printStackTrace();
			throw new Exception("Report Export Failed.");
		}

	}

	/*
	 * 
	 * 导出多个报表为rtf或word
	 * 
	 * @param JasperPrint,OutputStream,reportDataLists
	 *  @ 页面设置response.setContentType("application/msword");
	 *  @
	 * 页面设置response.setHeader("Content-disposition","attachment;filename=fileName.doc");
	 *  @ 页面设置response.setContentLength(bytes.length);
	 * 
	 */

	public byte[] generateWordOrRtfMultiple(String reportTitle,
			String reportFilePath, List reportDataLists) throws Exception {
		Map parameters = new HashMap();
		parameters.put("ReportTitle", reportTitle);// 报表标题
		JRRtfExporter rtfExporter = new JRRtfExporter();// word Rtf
		ByteArrayOutputStream oStream = new ByteArrayOutputStream();
		try {
			JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(
					reportDataLists);

			JasperPrint jasperPrint = JasperFillManager.fillReport(
					reportFilePath, parameters, ds);

			rtfExporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,
					jasperPrint);
			rtfExporter
			.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
			rtfExporter
					.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);
			rtfExporter.exportReport();
			byte[] bytes = oStream.toByteArray();
			return bytes;
		} catch (JRException e) {
			throw new Exception("Report Export Failed.");
		}

	}

	/*
	 * 
	 * 导出单个报表为CSV
	 * 
	 * @param JasperPrint,OutputStream,reportDataList
	 * 
	 */

	public byte[] generateCvs(String reportTitle, String reportFilePath,
			List reportDataLists) throws Exception {

		Map parameters = new HashMap();
		parameters.put("ReportTitle", reportTitle);// 报表标题
		JRCsvExporter csvExporter = new JRCsvExporter();
		ByteArrayOutputStream oStream = new ByteArrayOutputStream();
		try {
			JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(
					reportDataLists);

			JasperPrint jasperPrint = JasperFillManager.fillReport(
					reportFilePath, parameters, ds);

			csvExporter.setParameter(JRExporterParameter.JASPER_PRINT,
					jasperPrint);

			csvExporter
			.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
			
			csvExporter
					.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);

			csvExporter.exportReport();

			byte[] bytes = oStream.toByteArray();

			return bytes;

		} catch (JRException e) {
			throw new Exception("Report Export Failed.");
		}

	}
	/*

	 * 导出单个报表为XML

	 * @param JasperPrint,OutputStream

	 */
	public byte[] generateXML(String reportTitle, String reportFilePath,
			List reportDataList) throws Exception {
		Map parameters = new HashMap();
		parameters.put("ReportTitle", reportTitle);// 报表标题
		JRXmlExporter xmlExporter = new JRXmlExporter();
		ByteArrayOutputStream oStream = new ByteArrayOutputStream();
		try {
			JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(
					reportDataList);

			JasperPrint jasperPrint = JasperFillManager.fillReport(
					reportFilePath, parameters, ds);
			xmlExporter.exportReport();
			xmlExporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
			byte[] bytes = oStream.toByteArray();

			return bytes;

		} catch (JRException e) {
			e.printStackTrace();
			throw new Exception("Report Export Failed.");
		}
	}
}
